<template>
  <view class="cash-wrapper">

    <view class="card-form" style="height: 280rpx;">
      <view class="card-title">输入实名认证信息</view>
      <input class="card-input"  v-model="authentic.idCardName" type="text" placeholder="请输入真实姓名" placeholder-style="color:#CFCFCF;">

      <input class="card-input" v-model="authentic.idCardNum" type="text" placeholder="请输入身份证号码" placeholder-style="color:#CFCFCF;">

    </view>
    <view class="btn" style="margin-top: 56rpx;">
      <Button @click="userAuthentic">提交实名认证信息</Button>
    </view>
  </view>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, onUnmounted } from 'vue';
import Button from '@/components/Button.vue'
import api from '@/apis/api';
import {useRouter,useRoute} from 'vue-router'
import { showLoading, showToast } from '@/utils/prompt';

const authentic = reactive({
  idCardNum: null,
  idCardName: null,
})

function userAuthentic() {
    let msg = ''
    if (!authentic.idCardNum) msg = '请填写身份证号码'
    else if (!authentic.idCardName) msg = '请填写真实姓名'
    if (msg) return showToast(msg)
    if(authentic.idCardNum.length!=18){
      return showToast('请填写正确的身份证号码长度不正确')
    }
    if (!isCnNewID(authentic.idCardNum)){
      return showToast('请填写正确的身份证号码')
    }
  return api.userAuthentic(authentic).then(({ data }) => {
    showToast("身份认证成功！")
    uni.navigateTo({
      url:'/pages/user/cash/wallet'
    })
  })
}


function isCnNewID  (value: any) {
  var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子
  var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码
  if (/^\d{17}\d|x$/i.test(value)) {
    var sum = 0, idx;
    for (var i = 0; i < value.length - 1; i++) {
      // 对前17位数字与权值乘积求和
      sum += parseInt(value.substr(i, 1), 10) * arrExp[i];
    }
    // 计算模（固定算法）
    idx = sum % 11;
    // 检验第18为是否与校验码相等
    if (arrValid[idx] == value.substr(17, 1).toUpperCase()) {
      return true;
    } else {
      return true;
    }
  } else {
    return false;
  }
}

onMounted(() => {

})

onUnmounted(() => {

})

</script>

<style lang="scss" scoped>
page{
  background: #f9f9f9;
}
.cash-wrapper{
  padding: 0 30rpx;
  padding-top: 40rpx;
  font-family: PingFang SC Bold;

  .price-box{
    height: 300rpx;
    background: #ff5d58;
    border-radius: 32rpx;
    box-shadow: 0px 12rpx 12rpx 24rpx rgba(254, 112, 12, 0.07);
    font-family: PingFang SC Bold;
    display: flex;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    .price{
      font-size: 60rpx;
      color: #fff;
      .unit{
        font-size: 46rpx;
      }
    }
    .tips{
      font-size: 30rpx;
      color: #FFECEC;
    }
  }

  .card-form{
    background: #fff;
    border-radius: 30rpx;
    height: 230rpx;
    padding: 0 30rpx;
    margin-top: 48rpx;
    padding-top: 48rpx;
    .card-input{
      text-indent: 24rpx;
      font-size: 24rpx;
      border-radius: 16rpx;
      background: #F9F9F9;
      height: 80rpx;
      margin-top: 26rpx;
    }
    .card-title{
      color: #333333;
      font-size: 28rpx;
    }
    .tips{
      color: #979797;
      font-size: 28rpx;
      margin-top: 26rpx;
      .price{
        color: #ff0000;
      }
    }
  }
}
</style>
